<!--
  * hx-快速选型
  *
  * @Author:    xuesg
  * @Date:      2025-01-13 17:21:08
  * @Copyright  hxzmzz.tech
-->
<template>
  <a-modal
      :title="form.id ? '编辑' : '添加'"
      :width="1000"
      :open="visibleFlag"
      @cancel="onClose"
      :maskClosable="false"
      :destroyOnClose="true"
  >
    <a-form ref="formRef" :model="form" :rules="rules" :label-col="{ span: 5 }" >
        <a-form-item label="id"  name="id">
          <a-input-number style="width: 100%" v-model:value="form.id" placeholder="id" />
        </a-form-item>
        <a-form-item label="用户id"  name="accountId">
          <a-input-number style="width: 100%" v-model:value="form.accountId" placeholder="用户id" />
        </a-form-item>
        <a-form-item label="省id"  name="provinceId">
          <a-input-number style="width: 100%" v-model:value="form.provinceId" placeholder="省id" />
        </a-form-item>
        <a-form-item label="省"  name="province">
          <a-input style="width: 100%" v-model:value="form.province" placeholder="省" />
        </a-form-item>
        <a-form-item label="城市id"  name="cityId">
          <a-input-number style="width: 100%" v-model:value="form.cityId" placeholder="城市id" />
        </a-form-item>
        <a-form-item label="城市"  name="city">
          <a-input style="width: 100%" v-model:value="form.city" placeholder="城市" />
        </a-form-item>
        <a-form-item label="区id"  name="districtId">
          <a-input-number style="width: 100%" v-model:value="form.districtId" placeholder="区id" />
        </a-form-item>
        <a-form-item label="区"  name="district">
          <a-input style="width: 100%" v-model:value="form.district" placeholder="区" />
        </a-form-item>
        <a-form-item label="项目类型"  name="projectType">
          <a-input style="width: 100%" v-model:value="form.projectType" placeholder="项目类型" />
        </a-form-item>
        <a-form-item label="面积"  name="area">
          <a-input-number style="width: 100%" v-model:value="form.area" placeholder="面积" />
        </a-form-item>
        <a-form-item label="温度"  name="temperature">
          <a-input-number style="width: 100%" v-model:value="form.temperature" placeholder="温度" />
        </a-form-item>
        <a-form-item label="制冷量"  name="coolingCapacity">
          <a-input-number style="width: 100%" v-model:value="form.coolingCapacity" placeholder="制冷量" />
        </a-form-item>
        <a-form-item label="是否删除"  name="deletedFlag">
          <a-input-number style="width: 100%" v-model:value="form.deletedFlag" placeholder="是否删除" />
        </a-form-item>
    </a-form>

    <template #footer>
      <a-space>
        <a-button @click="onClose">取消</a-button>
        <a-button type="primary" @click="onSubmit">保存</a-button>
      </a-space>
    </template>
  </a-modal>
</template>
<script setup>
  import { reactive, ref, nextTick } from 'vue';
  import _ from 'lodash';
  import { message } from 'ant-design-vue';
  import { SmartLoading } from '/@/components/framework/smart-loading';
  import { selectionQuickApi } from './selection-quick-api';
  import { smartSentry } from '/@/lib/smart-sentry';

  // ------------------------ 事件 ------------------------

  const emits = defineEmits(['reloadList']);

  // ------------------------ 显示与隐藏 ------------------------
  // 是否显示
  const visibleFlag = ref(false);

  function show(rowData) {
    Object.assign(form, formDefault);
    if (rowData && !_.isEmpty(rowData)) {
      Object.assign(form, rowData);
    }
    // 使用字典时把下面这注释修改成自己的字典字段 有多个字典字段就复制多份同理修改 不然打开表单时不显示字典初始值
    // if (form.status && form.status.length > 0) {
    //   form.status = form.status.map((e) => e.valueCode);
    // }
    visibleFlag.value = true;
    nextTick(() => {
      formRef.value.clearValidate();
    });
  }

  function onClose() {
    Object.assign(form, formDefault);
    visibleFlag.value = false;
  }

  // ------------------------ 表单 ------------------------

  // 组件ref
  const formRef = ref();

  const formDefault = {
      id: undefined, //id
      accountId: undefined, //用户id
      provinceId: undefined, //省id
      province: undefined, //省
      cityId: undefined, //城市id
      city: undefined, //城市
      districtId: undefined, //区id
      district: undefined, //区
      projectType: undefined, //项目类型
      area: undefined, //面积
      temperature: undefined, //温度
      coolingCapacity: undefined, //制冷量
      deletedFlag: undefined, //是否删除
  };

  let form = reactive({ ...formDefault });

  const rules = {
      id: [{ required: true, message: 'id 必填' }],
      accountId: [{ required: true, message: '用户id 必填' }],
      provinceId: [{ required: true, message: '省id 必填' }],
      province: [{ required: true, message: '省 必填' }],
      cityId: [{ required: true, message: '城市id 必填' }],
      city: [{ required: true, message: '城市 必填' }],
      districtId: [{ required: true, message: '区id 必填' }],
      district: [{ required: true, message: '区 必填' }],
      projectType: [{ required: true, message: '项目类型 必填' }],
      area: [{ required: true, message: '面积 必填' }],
      temperature: [{ required: true, message: '温度 必填' }],
      coolingCapacity: [{ required: true, message: '制冷量 必填' }],
      deletedFlag: [{ required: true, message: '是否删除 必填' }],
  };

  // 点击确定，验证表单
  async function onSubmit() {
    try {
      await formRef.value.validateFields();
      save();
    } catch (err) {
      message.error('参数验证错误，请仔细填写表单数据!');
    }
  }

  // 新建、编辑API
  async function save() {
    SmartLoading.show();
    try {
      if (form.id) {
        await selectionQuickApi.update(form);
      } else {
        await selectionQuickApi.add(form);
      }
      message.success('操作成功');
      emits('reloadList');
      onClose();
    } catch (err) {
      smartSentry.captureError(err);
    } finally {
      SmartLoading.hide();
    }
  }

  defineExpose({
    show,
  });
</script>
